home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8735 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  5.5 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: What is &Variable (declared as: char Variable[10])?
  5. Date: Wed, 06 Mar 96 01:23:49 GMT
  6. Organization: none
  7. Message-ID: <826075429snz@genesis.demon.co.uk>
  8. References: <4gqpa1$3h9@alcor.usc.edu> <4gtab6$acb@ceylon.gte.com> <4gvksnINNnug@anvil.ugrad.cs.ubc.ca> <TANMOY.96Feb29100937@qcd.lanl.gov> <4hdtfcINN4l3@keats.ugrad.cs.ubc.ca>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <4hdtfcINN4l3@keats.ugrad.cs.ubc.ca>
  15.            c2a192@ugrad.cs.ubc.ca "Kazimir Kylheku" writes:
  16.  
  17. >In article <TANMOY.96Feb29100937@qcd.lanl.gov>,
  18. >Tanmoy Bhattacharya <tanmoy@qcd.lanl.gov> wrote:
  19.  
  20. >>except when it is the direct operand of the & operator or the direct
  21. >>operand of the sizeof operator. (You say that later: but that is much
  22. >>later :-).
  23. >
  24. >Well, the operand of a sizeof operator can be an expression, or a parenthesized
  25. >type name. If it is an expression, it is not evaluated, but only checked for
  26. >those attributes which determine the size of the lvalue. Conceptually, the
  27. >expression still has a value, albeit one which is thrown away. 
  28.  
  29. If the expression has a value to be thrown away it must have been evaluated
  30. However, as you say, it isn't. This is important where side-effects are
  31. concerned. What are performed are the type derivations which form part of
  32. normal expression evaluation.
  33.  
  34. >In the case of &, the operator does require the ``cast-expression''
  35. >syntactic unit, but again, it is interested in something other than the
  36. >straightforward value of the expression. The subject must be marked as an
  37. >lvalue that meets certain criteria. For all I know, in the expression
  38. >
  39. >        &myarray
  40. >
  41. >where myarray is an array of char, the myrray constituent could still be
  42. >treated as an expression that produces a pointer, a value that is ignored
  43. >because some other attribute of the expression is interesting to the &
  44. >operator.
  45.  
  46. It isn't necessary to generate values and then discard them, even
  47. conceptually (and the language definition doesn't). myarray here is
  48. a primary expression which 'evaluates' to an lvalue. An lvalue has a type
  49. and a reference to the underlying object. sizeof just derives a number from
  50. the type. & generates a pointer to the object using both type and reference
  51. (where an address is available, it isn't for example in an lvalue with
  52. register type). In something like:
  53.  
  54.      ptr = myarray;
  55.  
  56. the 'value' (or rvalue if you like) of the array lvalue is taken. The
  57. rules for taking the rvalue of an array lvalue are to generate a pointer
  58. to the array object's first element. ptr here is on the lhs of = which
  59. also requires an lvalue. = uses the type and reference of the lvalue to
  60. write a value to the object. ++ptr is interesting because it effectively
  61. treats ptr as both an lvalue and an rvalue (if you think of it as
  62. ptr = ptr+1)
  63.  
  64. >By the same reasoning, if I have an integer 'i' initialized to 3, and write
  65. >
  66. >        &i
  67. >
  68. >the i constituent is still an expression whose value is 3. However, the &
  69. >construct is interested in that i is an lvalue of a certain type and address,
  70. >and that it has an address, and the (conceptually present) computational value
  71. >is ignored.
  72.  
  73. The point is that i by itself is simply an lvalue which has int type and
  74. designates an int object (ISO 6.3.1). Only when you actually
  75. put it in a context that requires an rvalue is the value that the object holds
  76. read. i by itself is not an expression with the value 3.
  77.  
  78. >It's always useful to think of expressions and other constructs as not
  79. >computing only values, but also all kinds of other attributes, including type,
  80. >l- versus r- value markers and so forth, any of which can be the item of
  81. >interest to a dominating syntactic unit in favor of the value.
  82.  
  83. True, and it is the 'dominating syntactic unit' (which I will simply refer
  84. to as 'context') which determines whether an lvalue will be converted to an
  85. rvalue.
  86.  
  87. >In saying what the expression 'myarray' is, I should have been more thorough by
  88. >mentioning what other attributes it has, not just the principal computational
  89. >pointer value. Thus, here is my second attempt:
  90. >
  91. >myarray is an expression whose evaluation calls for the generation of a value,
  92. >whose type is pointer to char and which points to the first element of myarray.
  93. >However, the expression myrray also has other attributes. It is an lvalue which
  94. >can be the subject of sizeof() and & operators, which are not interested in
  95. >computing the pointer value,  but which look at the type and storage attributes
  96. >of myarray itself. It is not a modifiable lvalue, hence it cannot be assigned
  97. >to.
  98.  
  99. No, myarray is a non-modifiable lvalue whose type is array of char and which
  100. designates the object that is the entire array. Quantities such as a pointer
  101. to the array's first element can be derived from that where the context calls
  102. for them.
  103.  
  104. ...
  105.  
  106. >The bottom line is that it's not necessary to view the subjects of & and sizeof
  107. >as special cases; expressions are the same in the context of these operators as
  108. >they are anywhere else, and can be seen as producing the same values.
  109.  
  110. sizeof is special because it affects the entire evaluation process of its
  111. operand (i.e. inhibits everything bar type derivation). & like
  112. other operators simply performs actions using its operand's result.
  113.  
  114. -- 
  115. -----------------------------------------
  116. Lawrence Kirby | fred@genesis.demon.co.uk
  117. Wilts, England | 70734.126@compuserve.com
  118. -----------------------------------------
  119.